package com.LibreGeoSocial.App;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.LibreGeoSocial.Api.LibreGeoSocial;
import com.LibreGeoSocial.Api.User;

public class Register extends Activity implements OnClickListener {

	private Button btRegister;
	private TextView txtName;
	private TextView txtLastName;
	private TextView txtUsername;
	private TextView txtPassword;
	private TextView txtEmail;

	
	private static final int DIALOG_REGISTER_OK = 1;
	
    public void onCreate(Bundle icicle) {
    	
        super.onCreate(icicle);
        
        setContentView(R.layout.register);
        
        btRegister = (Button)this.findViewById(R.id.btRegister); 
        btRegister.setOnClickListener(this);
    
        txtName = (TextView) this.findViewById(R.id.txtName);
        txtLastName = (TextView) this.findViewById(R.id.txtLastname);
        
        txtUsername = (TextView) this.findViewById(R.id.txtUsername);
        txtPassword = (TextView) this.findViewById(R.id.txtPassword);
        txtEmail = (TextView) this.findViewById(R.id.txtEmail);

    
    }

	public void onClick(View v) {
		// TODO Auto-generated method stub
	
		User user = new User(0, txtName.getText().toString(),
								txtLastName.getText().toString(),
								txtUsername.getText().toString(),
								txtEmail.getText().toString(),
								"",0.0, 0.0, 0.0, 0.0, null,
								"ES",
								null,
								null,
								null,null,null);
								
		user.setPassword(txtPassword.getText().toString());
		
		
		
		if ( LibreGeoSocial.getInstance().registerUser(user) )
		{
    		
    		showDialog(DIALOG_REGISTER_OK);
    		
		}
		else
		{
    		Toast.makeText(getBaseContext(), 
					   "Error: " + LibreGeoSocial.getInstance().getLastMsgServer(), 
					   Toast.LENGTH_LONG).show();
		}
		
	}
	
    @Override
    protected Dialog onCreateDialog(int id) {
        
    	switch (id) {
    	
	    	case DIALOG_REGISTER_OK:
	    		
	    		LayoutInflater factory1 = LayoutInflater.from(this);
	    		final View textEntryView1 = factory1.inflate(R.layout.custom_dialog, null);
	    		
	    		TextView text = (TextView) textEntryView1.findViewById (R.id.dialog_text);
	    		ImageView image = (ImageView) textEntryView1.findViewById(R.id.dialog_image);
	
	    		text.setText("Congratulations!!\n\nThe registration was successful!\nPlease, go to Login Screen and log in to enjoy of LibreGeoSocial");
	
	    		
		        return new AlertDialog.Builder(this)	        
		        .setTitle("Register")		        
		        .setView(textEntryView1) 
		        .setIcon(R.drawable.logo)
		        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int whichButton) {
		            	
		            }
		        })
			    .create();
    	}
    	
    	return null;
    }
}
